﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Management.Automation;
using CrystalDecisions.Enterprise;
using CrystalDecisions.Enterprise.Dest;

namespace BO.Management.PowerShell
{

    [Cmdlet(VerbsCommon.Remove, "Notification", DefaultParameterSetName = CONNECT_SECWINAD)]
    public class RemoveNotification : BaseActionObject<ContentGenericProvider, InfoObject>
    {
        public override ContentGenericProvider GetProvider()
        {
            return new ContentGenericProvider(this.LogonProvider);
        }

        [Parameter(Position = 7, ParameterSetName = CONNECT_SECENTERPRISE, Mandatory = true)]
        [Parameter(Position = 5, ParameterSetName = CONNECT_SECWINAD, Mandatory = true)]
        public Notification NotificationType { get; set; }

        public override void WorkWithSingleObject(InfoObject boeObject)
        {

            //Select destations according notification type
            Destinations destinations;

            if (this.NotificationType == Notification.Failure)
            {
                WriteVerbose("Notification Target is notification on Failure");
                destinations = boeObject.SchedulingInfo.Notifications.DestinationsOnFailure;
            }
            else
            {
                WriteVerbose("Notification Target is notification on success");
                destinations = boeObject.SchedulingInfo.Notifications.DestinationsOnSuccess;
            }


            if (destinations != null)
            {

                if (destinations.Count == 0)
                {
                    WriteVerbose(String.Format("No {0} notification exist",this.NotificationType));
                    Console.WriteLine(String.Format("No {0} notification exist", this.NotificationType));
                }
                else
                {
                    WriteVerbose(String.Format("{0} {1} notification exist", this.NotificationType,destinations.Count));
                    while (destinations.Count > 0)
                    {
                        WriteVerbose(String.Format("{0} notification exist", destinations[1].Name));
                        Console.WriteLine(String.Format("Delete {0}", destinations[1].Name));
                        destinations.Delete(destinations[1]);
                    }
                }

                WriteVerbose("Save the instance with notification");
                BoeProvider.Commit(boeObject);
            }
            else
            {
                WriteVerbose("Destinations collection doesn't exist");
            }
            
            this.WriteObject(destinations);
        }
    }
}
